/**
 * 
 */
package org.swing.utility.jai.html;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import org.swing.utility.jai.imp.HTMLRenderer;

/**
 * @author lqnhu
 *
 */
public class HTMLRenderers {
	private static List<HTMLRenderer> renderers = new ArrayList<HTMLRenderer>();
	static {
		renderers.add(new ArrayRenderer());
	}

	private HTMLRenderers() {
	};

	public static List<HTMLRenderer> getRenderers() {
		return renderers;
	}

	public static void addRendered(HTMLRenderer renderer) {
		renderers.add(renderer);
	}

	private static class ArrayRenderer implements HTMLRenderer {
		public boolean canRender(Object o) {
			return o != null && o.getClass().isArray();
		}

		public String render(Object o) {
			int length = Array.getLength(o);
			StringBuffer sb = new StringBuffer();
			sb.append("[");
			for (int i = 0; i < length; i++) {
				sb.append(HTMLBuilder.render(Array.get(o, i)));
				if (i < length - 1)
					sb.append(", ");
				else
					sb.append("]");
			}
			return sb.toString();
		}
	}
}
